const Koa = require( "koa" );
const fs = require( "fs" );
const path = require( "path" );
const app = new Koa();
const cors = require( 'koa2-cors' );
const { v4 } = require( 'uuid' );
const ip = require( 'ip' );

const router = require( "koa-router" )( {
	prefix: "/api"
} );
app.use( cors() );
const koaBody = require( "koa-body" ); //解析上传文件的插件
app.use(
	koaBody( {
		multipart: true,
		formLimit: "100mb",
		jsonLimit: "100mb",
		textLimit: "100mb",
		enableTypes: [ "json", "form", "text" ],
		formidable: {
			multipart: true,
			maxFileSize: 100 * 1024 * 1024
		}
	} )
);
const static = require( "koa-static" );

// 模拟存库
let pageData = {}
// 渠道
let uuid = v4()
let worksChannels = [
	{
		channelId: uuid,
		channelName: "默认",
		url: `http://${ ip.address() }:8089/#/?channelId=${ uuid }`,
	}
]
app.use( static( path.resolve( "./mock" ) ) );
function uploadHandler ( file, urls ) {
	const uploadFile = path.resolve( "./mock/upload" );
	if ( !fs.existsSync( uploadFile ) ) {
		fs.mkdirSync( uploadFile );
	}
	const reader = fs.createReadStream( file.path );
	const ext = file.name.split( "." ).pop();
	const fileName = `${ v4() }.${ ext }`;
	const upStream = fs.createWriteStream( `${ uploadFile }/${ fileName }` );
	reader.pipe( upStream );
	urls.push( `http://${ ip.address() }:7878/upload/${ fileName }` );
}

// 图片上传
router.post( "/upload", async ctx => {
	const file = ctx.request.files.file;
	let urls = [];
	if ( Array.isArray( file ) ) {
		file.map( f => {
			uploadHandler( f, urls );
		} );
	} else {
		uploadHandler( file, urls );
	}

	return ( ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: {
			urls
		}
	} );
} );

// 保存作品
router.post( "/works", async ctx => {
	pageData = ctx.request.body
	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: null
	};
} );

// 获取作品
router.get( "/works", async ctx => {
	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: pageData
	};
} );

// 获取渠道
router.get( "/worksChannels", async ctx => {
	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: worksChannels
	};
} );

// 新增渠道
router.post( "/worksChannel", async ctx => {
	let { channelName } = ctx.request.body
	let channelId = v4();
	worksChannels.push( {
		channelId,
		channelName: channelName,
		url: `http://${ ip.address() }:8089/#/?channelId=${ channelId }`,
	} )

	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: null
	};
} );

// 删除渠道
router.delete( "/worksChannel/:channelId", async ctx => {
	const channelId = ctx.params.channelId;
	worksChannels = worksChannels.filter( item => item.channelId !== channelId )
	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: null
	};
} );

// 获取本地ip
router.get( "/localIp", async ctx => {
	ctx.body = {
		success: true,
		code: "000000",
		errorMessage: "成功",
		data: {
			ip: ip.address()
		}
	};
} );

app.use( router.routes() );
app.use( router.allowedMethods() );

app.listen( 7878, () => {
	console.log( "服务已启动端口:7878" );
} );
